using Coldairarrow.Util;
using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Cache
{
  public class StringCache : IStringCache, ITransientDependency
  {
    readonly IDistributedCache _cache;
    public StringCache(IDistributedCache cache)
    {
      _cache = cache;
    }

    public async Task<string> GetCacheAsync(string key)
    {
      if (key.IsNullOrEmpty())
        return null;
      var cache = await _cache.GetStringAsync(key);
      return cache;
    }

    public async Task RemoveAsync(string key)
    {
      await _cache.RemoveAsync(key);
    }

    public async Task SetCacheAsync(string key, string value, TimeSpan timeSpan)
    {
      if (key.IsNullOrEmpty())
        throw new ArgumentNullException("key is Null");
      DistributedCacheEntryOptions options = new DistributedCacheEntryOptions();
      options.AbsoluteExpirationRelativeToNow = timeSpan;
      await _cache.SetStringAsync(key, value,options);
    }
  }
}
